#ifndef BATTERY_H
#define BATTERY_H

#include <QWidget>
#include <QPainter>
#include <QTimer>


class Battery : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(double minValue READ minValue WRITE setMinValue)
    Q_PROPERTY(double maxValue READ maxValue WRITE setMaxValue)
    Q_PROPERTY(double value READ value WRITE setValue)
    Q_PROPERTY(double alarmValue READ alarmValue WRITE setAlarmValue)
    Q_PROPERTY(double step READ step WRITE setStep)

    Q_PROPERTY(QColor borderColorStart READ borderColorStart WRITE setBorderColorStart)
    Q_PROPERTY(QColor borderColorEnd READ borderColorEnd WRITE setBorderColorEnd)

    Q_PROPERTY(QColor alarmColorStart READ alarmColorStart WRITE setAlarmColorStart)
    Q_PROPERTY(QColor alarmColorEnd READ alarmColorEnd WRITE setAlarmColorEnd)

    Q_PROPERTY(QColor normalColorStart READ normalColorStart WRITE setNormalColorStart)
    Q_PROPERTY(QColor normalColorEnd READ normalColorEnd WRITE setNormalColorEnd)

public:
    explicit Battery(QWidget *parent = nullptr);
    ~Battery();

public:
    double minValue() const { return _minValue; }
    double maxValue() const { return _maxValue; }
    double value() const { return _value; }
    double alarmValue() const { return _alarmValue; }
    double step() const { return _step; }

    QColor borderColorStart() const { return _borderColorStart; }
    QColor borderColorEnd() const { return _borderColorEnd; }

    QColor alarmColorStart() const { return _alarmColorStart; }
    QColor alarmColorEnd() const { return _alarmColorEnd; }

    QColor normalColorStart() const { return _normalColorStart; }
    QColor normalColorEnd() const { return _normalColorEnd; }

    QSize sizeHint() const { return QSize(150, 80); }
    QSize minimumSizeHint() const { return QSize(30,10); }

signals:
    void valueChanged(double value_);

public slots:
    //设置范围值
    void setRange(double minValue_, double maxValue_);
    void setRange(int minValue_, int maxValue_);

    //设置最大最小值
    void setMinValue(double minValue_);
    void setMaxValue(double maxValue_);

    //设置电池电量值
    void setValue(double value_);
    void setValue(int value_);

    //设置电池电量警戒值
    void setAlarmValue(double alarmValue_);
    void setAlarmValue(int alarmValue_);

    //设置步长
    void setStep(double step_);
    void setStep(int step_);

    //设置边框渐变颜色
    void setBorderColorStart(const QColor &_borderColorStart);
    void setBorderColorEnd(const QColor &_borderColorEnd);

    //设置电池电量报警时的渐变颜色
    void setAlarmColorStart(const QColor &_alarmColorStart);
    void setAlarmColorEnd(const QColor &_alarmColorEnd);

    //设置电池电量正常时的渐变颜色
    void setNormalColorStart(const QColor &_normalColorStart);
    void setNormalColorEnd(const QColor &_normalColorEnd);

protected:
    void paintEvent(QPaintEvent *);
    void drawBorder(QPainter* painter);
    void drawBg(QPainter* painter);

private slots:
    void updateValue();

private:
    double _minValue;   //最小值
    double _maxValue;   //最大值
    double _value;      //目标电量
    double _alarmValue; //电池电量警戒值

    double _step;       //每次移动的步长

    QColor _borderColorStart;   //边框渐变开始颜色
    QColor _borderColorEnd;     //边框渐变结束颜色

    QColor _alarmColorStart;    //电池低电量时的渐变开始颜色
    QColor _alarmColorEnd;      //电池低电量时的渐变结束颜色

    QColor _normalColorStart;   //电池正常电量时的渐变开始颜色
    QColor _normalColorEnd;     //电池正常电量时的渐变结束颜色

    bool isForward;         //是否往前移
    double currentValue;    //当前电量
    QRectF batteryRect;     //电池主体区域
    QTimer* timer;          //绘制定时器
};

#endif // BATTERY_H
